" Vim with all enhancements

" 平台判断
let g:isWin = 0
if (has("win32") || has("win95") || has("win64") || has("win32"))
	let g:isWin = 1
endif

" 设置行号
set nu
augroup relative_numbser
 autocmd!
 autocmd InsertEnter * :set norelativenumber
 autocmd InsertLeave * :set relativenumber
augroup END

"兼容性vi
set nocompatible
set backspace=2

"设置文件的代码形式 utf8
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf-8,chinese,cp936
"vim的菜单乱码解决
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
 
"vim提示信息乱码的解决
language messages zh_CN.utf-8

" 基础设置

" 基础设置
set history=500		"保留历史记录
set guifont=Monaco:h10	"设置字体为Monaco，大小10
set scrolloff=5 	"在光标接近底端或顶端时，自动下滚或上滚
set autoread 		"设置当文件在外部被修改，自动更新该文件
set mouse=a 		"设置在任何模式下鼠标都可用
set smartindent "智能对齐
set shiftwidth=4 "换行时，交错使用4个空格
set tabstop=4 "tab为4个空格
"set autoindent "设置自动对齐
"set ai! "设置自动缩进
"set cursorcolumn "启用光标列
" set cursorline	"启用光标行
" set guicursor+=a:blinkon0 "设置光标不闪烁
" set fdm=indent "
set nobackup " 不需要备份文件*.*~
set go= " 去除菜单栏

set undodir = "~/.undodir"

:let mapleader = "@"

" 快捷键设置
nmap <space> :w<enter>
 
"  快速插入
imap () ()<Left>

imap [] []<Left>

imap {} {}<Left>

imap "" ""<Left>
imap <> <><Left>


" 快速切窗口
nmap \ <c-w>w
nmap <tab> <c-w>w
"  快速展开全部代码
nmap - zO 
"  快速插入代码结束符号
imap ;; <esc>A;<esc>
"  快速插入花括号
imap {}<enter> <esc>i{}<esc>a<enter><esc>O
"  快捷插入注释符
nmap <c-k> I//<space><esc>
imap <c-k> <esc>I//<space><esc>
vmap <c-k> I// <esc>
vmap <c-c> "+y
vnoremap FF y/<c-r>"<cr>

"  自动命令
augroup MyAutoCMD
	autocmd!
	"  自动判断logcat文件
	au BufRead,BufNewFile *.logcat set filetype=logcat
augroup END

nmap <leader>g :!gitui<enter>

let g:openOmniSharp = 1

" 设置插件
call plug#begin('~/.vim/plugged')
Plug 'joshdick/onedark.vim'

" MarkDown
" Plug 'godlygeek/tabular' "必要插件，安装在vim-markdown前面
" Plug 'plasticboy/vim-markdown'
" Plug 'iamcco/markdown-preview.vim'

" 语法
" Plug 'prabirshrestha/asyncomplete.vim'
" Plug 'dense-analysis/ale'
if (openOmniSharp == 1)
Plug 'OmniSharp/omnisharp-vim'
endif
" 需要安装NodeJS, 以及对应语言服务器coc-tsserver,
" 可以直接适配OmniShaper无需安装C#对应
" 需要修改配置文件(命令:CocConfig 可以打开){"suggest.noselect": true}, 否则无法正常用tab
Plug 'neoclide/coc.nvim', {'branch': 'release'}

" 功能插件
" FZF 需要安装ripgrep,windows 需要安装 iwr -useb get.scoop.sh | iex
" 后安装rg，之后重启
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'

Plug 'scrooloose/nerdtree'
Plug 'jistr/vim-nerdtree-tabs'

Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'

" 语法高亮
Plug '~/.vim/plugged/ccceffect'
Plug 'kalvinpearce/ShaderHighlight'
" Plug 'sheerun/vim-polyglot'

" 代码插入
call plug#end()

" 配色
colorscheme onedark 

let g:airline_theme='onedark'
autocmd vimenter * ++nested colorscheme onedark
" set bg=dark
" set background=dark

" Use truecolor in the terminal, when it is supported
if has('termguicolors')
  set termguicolors
endif

if exists('+termguicolors')
  let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
  let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
  set termguicolors
endif

" }}}

" Settings: {{{
filetype indent plugin on
if !exists('g:syntax_on') | syntax enable | endif
set encoding=utf-8
scriptencoding utf-8
" }}}



" Airline
let g:airline#extensions#tabline#enabled = 1


" 关闭NERDTree快捷键
map <leader>t :NERDTreeToggle<CR>
" 进入目录后直接切换
let g:NERDTreeChDirMode = 2
" 显示行号
let NERDTreeShowLineNumbers=1
" let NERDTreeAutoCenter=1
" 是否显示隐藏文件
let NERDTreeShowHidden=1
" 设置宽度
let NERDTreeWinSize=40
" 在终端启动vim时，共享NERDTree
let g:nerdtree_tabs_open_on_console_startup=1
" 忽略一下文件的显示
let NERDTreeIgnore=['\.pyc','\~$','\.swp','\.meta']
" 显示书签列表
let NERDTreeShowBookmarks=1

" fzf 设置
noremap <c-p> :Buffers<CR>
noremap <leader>p :FZF<CR>
" noremap \ :FZF<CR>
noremap \ :QFiles<CR>
let g:fzf_layout = { 'up': '~40%' }
"  优先打开buffer的文件
let g:fzf_buffers_jump = 1
"  搜索进入
let g:fzf_action = {
  \ 'enter': 'edit',
  \ 'ctrl-x': 'split',
  \ 'ctrl-v': 'vsplit' }
" 搜索文本
let g:rg_command = '
  \ rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --color "always"
  \ -g "*.{json,cs,ts,effect,chunk}"
  \ -g "!{.git,node_modules,vendor,build,library,temp,settings,packages,local}/*" '

command! -bang -nargs=* F call fzf#vim#grep(g:rg_command .shellescape(<q-args>), 1, <bang>0)

let g:fd_command = 'fd --type f -e cs -e json -e ts -e effect -e chunk --exclude={.git,node_modules,vendor,build,library,temp,settings,packages,local}'
command! -bang -nargs=* QFiles call fzf#run(fzf#wrap({'source': g:fd_command})))

" coc
let g:coc_global_extensions = ['coc-json', 'coc-tsserver']
" Some servers have issues with backup files, see #649.
set nobackup
set nowritebackup

" Having longer updatetime (default is 4000 ms = 4 s) leads to noticeable
" delays and poor user experience.
set updatetime=300

" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved.
set signcolumn=yes

inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

" Use <c-space> to trigger completion.
if has('nvim')
  inoremap <silent><expr> <c-space> coc#refresh()
else
  inoremap <silent><expr> <c-@> coc#refresh()
endif

" nmap <silent> <c-[> <Plug>(coc-diagnostic-prev)
nmap <silent> <leader>e <Plug>(coc-diagnostic-next)


" GoTo code navigation.
nmap <silent> <leader>i <Plug>(coc-definition)
nmap <silent> <leader>tt <Plug>(coc-type-definition)
nmap <silent> <leader>fi <Plug>(coc-implementation)
nmap <silent> <leader>fu <Plug>(coc-references)

" Use K to show documentation in preview window.
nnoremap <silent> <leader>dc :call ShowDocumentation()<CR>

function! ShowDocumentation()
  if CocAction('hasProvider', 'hover')
    call CocActionAsync('doHover')
  else
    call feedkeys('K', 'in')
  endif
endfunction

" Highlight the symbol and its references when holding the cursor.
" autocmd CursorHold * silent call CocActionAsync('highlight')

" Symbol renaming.
nmap <leader>rn <Plug>(coc-rename)

" Formatting selected code.
xmap <leader>f  <Plug>(coc-format-selected)
nmap <leader>f  <Plug>(coc-format-selected)

" Applying codeAction to the selected region.
" Example: `<leader>aap` for current paragraph
xmap <leader>a  <Plug>(coc-codeaction-selected)
nmap <leader>a  <Plug>(coc-codeaction-selected)
" Remap keys for applying codeAction to the current buffer.
" nmap <leader>ac  <Plug>(coc-codeaction)
" Apply AutoFix to problem on the current line.
" nmap <leader>qf  <Plug>(coc-fix-current)

" Run the Code Lens action on the current line.
" nmap <leader>cl  <Plug>(coc-codelens-action)

" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server.
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)

" Use CTRL-S for selections ranges.
" Requires 'textDocument/selectionRange' support of language server.
" nmap <silent> <C-s> <Plug>(coc-range-select)
" xmap <silent> <C-s> <Plug>(coc-range-select)

 
" OmniSharp 设置
if (openOmniSharp == 1)
	let g:OmniSharp_server_stdio = 1 
	" let g:OmniSharp_start_server = 0
	" 解决win gvim C#代码经常找不到文件
	if (isWin == 1)
	set hidden
	else
	let g:OmniSharp_server_use_mono = 1
	endif
	
	let g:OmniSharp_selector_ui = 'fzf'    " Use fzf
	let g:OmniSharp_selector_findusages = 'fzf'
	let g:OmniSharp_selector_findmember = 'fzf'
	" 关闭自动高量，打开设为2
	let g:OmniSharp_highlighting = 2
	augroup omnisharp_commands
	    autocmd!
		" 高亮
	    autocmd FileType cs nnoremap <buffer> <Leader>h :OmniSharpHighlight<CR>
	
	    " Show type information automatically when the cursor stops moving.
	    " Note that the type is echoed to the Vim command line, and will overwrite
	    " any other messages in this space including e.g. ALE linting messages.
	    " autocmd CursorHold *.cs OmniSharpTypeLookup
	
	    " The following commands are contextual, based on the cursor position.
	    " autocmd FileType cs nnoremap <buffer> <Leader>i :OmniSharpGotoDefinition<CR>
	    autocmd FileType cs nnoremap <buffer> <Leader>fi :OmniSharpFindImplementations<CR>
	    autocmd FileType cs nnoremap <buffer> <Leader>fs :OmniSharpFindSymbol<CR>
	    " autocmd FileType cs nnoremap <buffer> <Leader>fu :OmniSharpFindUsages<CR>
	
	    " Finds members in the current buffer
	    autocmd FileType cs nnoremap <buffer> <Leader>fm :OmniSharpFindMembers<CR>
	
	    autocmd FileType cs nnoremap <buffer> <Leader>fx :OmniSharpFixUsings<CR>
	    autocmd FileType cs nnoremap <buffer> <Leader>tt :OmniSharpTypeLookup<CR>
	    " autocmd FileType cs nnoremap <buffer> <Leader>dc :OmniSharpDocumentation<CR>
	    autocmd FileType cs nnoremap <buffer> <Leader>rs :OmniSharpRestartServer<CR>
		autocmd FileType cs nmap <silent> <buffer> <Leader>qq <Plug>(omnisharp_preview_definition)
	  	autocmd FileType cs nmap <silent> <buffer> <Leader>qw <Plug>(omnisharp_preview_implementations)
	    autocmd FileType cs nnoremap <buffer> <C-\> :OmniSharpSignatureHelp<CR>
	    autocmd FileType cs inoremap <buffer> <C-\> <C-o>:OmniSharpSignatureHelp<CR>
	
	    " Navigate up and down by method/property/field
	    autocmd FileType cs nnoremap <buffer> <C-k> :OmniSharpNavigateUp<CR>
	    autocmd FileType cs nnoremap <buffer> <C-j> :OmniSharpNavigateDown<CR>
	
	    " Find all code errors/warnings for the current solution and populate the quickfix window
	    autocmd FileType cs nnoremap <buffer> <Leader>cc :OmniSharpGlobalCodeCheck<CR>
	augroup END
endif



























